package com.soft.sitegenerator.action;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.Role;
import com.soft.sitegenerator.business.hibernate.Profile;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.ProfileEditForm;

public class ProfileEditAction extends DispatchAction {

	public ActionForward editProfile(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		ProfileEditForm profileEditForm = (ProfileEditForm) form;
		
	    Long id = new Long(Long.parseLong(request.getParameter("id")));
		
	    Profile profile = profileEditForm.getProfile();
	    profile.setId(id);

	    try {
	    	profile.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
	    
		Component component=new DataBaseComponent();
   	    Role role = (Role) component.getBean("rolesHib");
   	    
   	    Collection var1 = role.findAll();
   	    
		profileEditForm.setRoles(var1);
		
		profileEditForm.setIdRole(profile.getRole().getId());
	    
		return mapping.findForward("showEdit");
	}
	
	public ActionForward deleteProfile(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		ProfileEditForm profileEditForm = (ProfileEditForm) form;

	    Long id = new Long(Long.parseLong(request.getParameter("id")));
		
	    profileEditForm.getProfile().setId(id);
	    profileEditForm.getProfile().delete();
		
		return mapping.findForward("showList");
	}
	
	public ActionForward addProfile(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		
		ProfileEditForm profileEditForm = (ProfileEditForm) form;
		
		Component component=new DataBaseComponent();
   	    Role role = (Role) component.getBean("rolesHib");
   	    
   	    Collection var1 = role.findAll();
   	    
		profileEditForm.setRoles(var1);
	    
		return mapping.findForward("showAdd");
	}
	
	public ActionForward saveProfile(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		ProfileEditForm profileEditForm = (ProfileEditForm) form;
		
		Component component=new DataBaseComponent();
   	    Role role = (Role) component.getBean("rolesHib");
		role.setId(profileEditForm.getIdRole());
		
		try {
			role.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
   	    
	    Profile profile = profileEditForm.getProfile();
		profile.setRole(role);
	    profile.save();
		
		return mapping.findForward("showList");
		
	}

	public ActionForward updateProfile(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			ProfileEditForm profileEditForm = (ProfileEditForm) form;
			
			Component component=new DataBaseComponent();
	   	    Role role = (Role) component.getBean("rolesHib");
			role.setId(profileEditForm.getIdRole());
			
			try {
				role.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}
	   	    
		    Profile profile = profileEditForm.getProfile();
			profile.setRole(role);
			profile.update();
			
			return mapping.findForward("showList");
			
		}

}